
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * Servlet implementation class cutting
 */
@WebServlet("/cutting")
public class cutting extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public cutting() {
        super();
        // TODO Auto-generated constructor stub
    }
   
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String str = request.getParameter("url");
		String key = request.getParameter("key");
		String http = " ";
		String tmp = " ";
		char ch;
		int k = 0;
		int i , j;
		boolean firstq=true;
		int countarry = 1 ;
		out.println(str.length() +"<br>");
		while(k!=str.length())
		{
			ch = str.charAt(k);
			if(ch=='?'&& firstq)
			{
				for(i = 0; i < k; i++)
				{
					http= http + String.valueOf(str.charAt(i));
				}
				for(i = k+1; i < str.length(); i++)
				{
					tmp = tmp + String.valueOf(str.charAt(i));
				}
				break;
			}
			k++;
		}
		String[] strstr = tmp.split("&");
		//String key="ȫ�浿";
		out.println("");
		out.println(key+"<br>");
		out.println(http+"<br>");
		for(j=0; j < strstr.length; j++)
			out.println(strstr[j]+"<br>");
		out.println("");
		//out.println(str);
		backt backt= new backt(str, key, http, strstr);
		int select[] = new int[strstr.length];
		backt.bt(0, select.length, select);
		out.println(backt.hap2());
		out.println("");
		out.println(""); 
		out.close();
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		String str = request.getParameter("url");
		String key = request.getParameter("key");
		String option = request.getParameter("option");
		
		
		String http = "";
		String tmp = "";
		char ch;
		int k = 0;
		int i , j;
		boolean firstq=true;
		int countarry = 1 ;
		out.println(str.length() +"<br>");
		while(k!=str.length())
		{
			ch = str.charAt(k);
			if(ch=='?'&& firstq)
			{
				for(i = 0; i < k; i++)
				{
					http= http + String.valueOf(str.charAt(i));
				}
				for(i = k+1; i < str.length(); i++)
				{
					tmp = tmp + String.valueOf(str.charAt(i));
				}
				break;
			}
			k++;
		}
		String[] strstr = tmp.split("&");
		//String key="ȫ�浿";
		out.println(option+"<br>");
		out.println("키워드값 : "+ key+"<br>");
		out.println("홈페이지 주소: "+ http+"<br>");
		for(j=0; j < strstr.length; j++)
			out.println("키워드요소"+ j +" : "+strstr[j]+"<br>");
		out.println("");
		out.println("입력한  URL: "+"<br>"+str+"<br>");
		//backt backt= new backt(str, key, http, strstr);
		//int select[] = new int[strstr.length];
		//backt.bt(0, select.length, select);
		//out.println("결과 검색 조합 URL: "+"<br>"+backt.hap2());
		//out.println("<br>");
		out.println(""); 
		
		search_pro pro= new search_pro(str, key, http, strstr);
		pro.newbt();
		out.println("결과 검색 조합 URL.1: "+"<br>"+pro.hap2());
		pro.ktype();
		pro.type(option);
		pro.replaceboth();
		
		
		out.println("<br>사용가능 검색 분류:");
		out.println(pro.str[1]);
		System.out.println("-------끝-------");
		out.close();
		
	}

}
